package com.medical.mapi.service.impl;

import com.medical.mapi.mapper.UserMapper;
import com.medical.mapi.model.User;
import com.medical.mapi.service.UserException;
import com.medical.mapi.service.UserService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * Created by junzi on 2016/10/17.
 */
@Service("userService")
public class UserServiceImpl implements UserService {
    private static final Logger LOGGER = LoggerFactory.getLogger(UserService.class);

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RestTemplate restTemplate;

    @Override
    public List<User> findAllList() {
        return userMapper.selectList();
    }

    @HystrixCommand(fallbackMethod = "fallback")
    @Override
    public User findById(Long id) {
        return restTemplate.getForObject("http://provider-user/" + id, User.class);
    }


    public User fallback(Long id) {
        LOGGER.info("发生异常，进入fallback方法，接收的参数：id = " + id);
        return new User();
    }

}
